/**
 * OSS is a Open Service Container which gives the leverage of building the Desktop and Web
 * Application. * Copyright (C) 2008-2009  Ponraj Sutanthiramani
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Or5e Open Source Community
 * 105, S.S Paradise Apartment, Munnireddy Layout, Horamavu, Banaswadi, Bangalore, India.
 */
package org.or5e.api.webcam.images;

import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.MemoryImageSource;

import org.or5e.api.webcam.util.ElapsedTimer;

// Referenced classes of package images:
//            Array2dTestFrame, RectangleGrabber, FileGrabber, PixelArray

public class Array2dComponent extends Canvas
{
	private static final long serialVersionUID = 1L;
	private Image memImage;
    private int pixels[];
    private byte bytes[];
    private int width;
    private int height;
    Graphics gstore;
    MemoryImageSource mis;
    int mag;
    static int DEFAULT_MAG = 1;

    public Array2dComponent(byte b[][])
    {
        this(b, DEFAULT_MAG);
    }

    public Array2dComponent(byte b[][], int mag)
    {
        this.mag = mag;
        setBytes(b);
    }

    public Array2dComponent(byte b[], int width, int height)
    {
        this(b, width, height, DEFAULT_MAG);
    }

    public Array2dComponent(byte b[], int width, int height, int mag)
    {
        this.mag = mag;
        this.width = width;
        this.height = height;
        pixels = new int[b.length];
        setBytes(b);
        gstore = null;
    }

    public Array2dComponent(int pixels[], int width, int height, int mag)
    {
        this.mag = mag;
        this.width = width;
        this.height = height;
        setPixels(pixels);
        gstore = null;
    }

    public Array2dComponent(int width, int height, int mag)
    {
        this(new byte[width * height], width, height, mag);
    }

    public Array2dComponent(int width, int height)
    {
        this(width, height, DEFAULT_MAG);
    }

    public void setPixels(int pixels[])
    {
        this.pixels = pixels;
        setImage();
    }

    public void setBytes(byte b[][])
    {
        width = b.length;
        height = b[0].length;
        bytes = new byte[width * height];
        pixels = new int[width * height];
        for(int i = 0; i < pixels.length; i++)
        {
            int x = b[i % width][i / width];
            pixels[i] = 0xff000000 + x + (x << 8) + (x << 16);
        }

        setImage();
    }

    public byte[][] getByteArray()
    {
        return RectangleGrabber.getBytes(pixels, width, height);
    }

    public void setBytes(byte b[])
    {
        System.out.println("Setting bytes: " + b[0]);
        bytes = b;
        if(pixels == null || b.length != pixels.length)
            pixels = new int[b.length];
        for(int i = 0; i < b.length; i++)
        {
            int x = b[i] + 128;
            pixels[i] = 0xff000000 + x + (x << 8) + (x << 16);
        }

        setImage();
    }

    private void setImage()
    {
        if(mis == null || memImage == null)
        {
            mis = new MemoryImageSource(width, height, pixels, 0, width);
            mis.setAnimated(true);
            memImage = createImage(mis);
        } else
        {
            mis.newPixels();
        }
    }

    public Image getImage()
    {
        if(memImage == null)
            setImage();
        return memImage;
    }

    public byte[] getBytes()
    {
        return bytes;
    }

    public int[] getPixels()
    {
        return pixels;
    }

    @Override
    public void paint(Graphics g)
    {
        if(memImage != null)
            g.drawImage(memImage, 0, 0, width * mag, height * mag, null);
    }

    @Override
    public void update(Graphics g)
    {
        paint(g);
    }

    public void redraw()
    {
        if(gstore == null)
            gstore = getGraphics();
        if(memImage != null)
            gstore.drawImage(memImage, 0, 0, width * mag, height * mag, null);
    }

    @Override
    public Dimension getPreferredSize()
    {
        return new Dimension(width * mag, height * mag);
    }

    public static void main(String args[])
    {
        System.out.println(-1);
        byte bt = -1;
        System.out.println(255 + bt);
//        String file = "C:/sml/viadocs/images/blue.jpg";
        ElapsedTimer et = new ElapsedTimer();
//        PixelArray pa = FileGrabber.getPixels(file);
        System.out.println(et);
        et.reset();
//        byte b[][] = pa.greyBytes();
        System.out.println(et);
//        Array2dComponent ac = new Array2dComponent(pa.pixels, pa.width, pa.height, 1);
    }

}